package com.example.lib_http

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitManager {
    companion object{
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED){
            RetrofitManager()
        }
        var token:String?=null
    }
    private fun getOkhttp():OkHttpClient{
        return OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor{it.proceed(token?.let { e->it.request().newBuilder().addHeader("token",e).build() }?:it.request())}
            .build()
    }
   private fun getRetrofit(): Retrofit {
        return Retrofit.Builder()
            .client(getOkhttp())
            .baseUrl("http://10.161.9.80:7015/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
    fun <T> create(clazz:Class<T>):T=getRetrofit().create(clazz)
}